/**
 * 
 */
package com.lehecai.core.lottery;

import com.lehecai.core.IntegerBeanLabelItem;
import com.lehecai.core.api.lottery.Plan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Sunshow
 * 方案状态
 */
public class PlanStatus extends IntegerBeanLabelItem {

	private static final long serialVersionUID = 8815493451454402080L;

	private static final Logger logger = LoggerFactory.getLogger(PlanStatus.class.getName());
	
	private static List<PlanStatus> items = new ArrayList<PlanStatus>();
	
	protected PlanStatus(String name, int value) {
		super(PlanStatus.class.getName(), name, value);
		items.add(this);
	}
	
	public static PlanStatus getItem(int value){
		try {
			return (PlanStatus)PlanStatus.getResult(PlanStatus.class.getName(), value);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		}
	}
	
	public static List<PlanStatus> getItems() {
		return items;
	}

    /**
     * 获取方案回收的中间态
     * @param plan 方案信息，原始状态、上传状态等信息
     * @return 进行回收需要设置的中间状态
     */
    public static PlanStatus getRecycleProcessingStatus(Plan plan) {
        if (plan == null || plan.getPlanStatus() == null) {
            logger.error("传入数据为空");
            return null;
        }

        PlanStatus srcStatus = plan.getPlanStatus();

        PlanStatus desStatus = null;

        if (srcStatus.getValue() == PlanStatus.PRINT_WAITING.getValue()) {

            // 可能是未上传，进行判断
            if (plan.getSelectType() != null
                    && plan.getSelectType().getValue() == SelectType.UPLOAD.getValue()
                    && plan.getUploadId() == 0) {
                // 未上传，设置成未上传作废
                desStatus = PlanStatus.NOTUPLOAD_OBSOLETE_REFUNDING;
            } else {
                desStatus = PlanStatus.UNPRINTED_OBSOLETE_REFUNDING;
            }
        } else if (srcStatus.getValue() == PlanStatus.PRINTING.getValue()) {
            desStatus = PlanStatus.PRINTED_FAILED_REFUNDING;
        } else if (srcStatus.getValue() == PlanStatus.PAID_NOT.getValue()) {
            desStatus = PlanStatus.NOTPAID_OBSOLETING;
        } else if (srcStatus.getValue() == PlanStatus.RECRUITING.getValue()) {

            if (plan.getPlanTicketStatus().getValue() == PlanTicketStatus.SPLIT_COMPLETED.getValue()
                    || plan.getPlanTicketStatus().getValue() == PlanTicketStatus.CAN_SPLIT.getValue()) {
                // 如果已拆票或可拆票状态，说明是后台手动强制撤单，设置成已撤单
                desStatus = PlanStatus.CANCELLING;
            } else {
                // 对于不可拆票状态的方案，有两种可能，1-未上传方案，2-未达到进度+保底>=90%
                if (plan.getSelectType() != null
                        && plan.getSelectType().getValue() == SelectType.UPLOAD.getValue()
                        && plan.getUploadId() == 0) {
                    // 未上传，设置成未上传作废
                    desStatus = PlanStatus.NOTUPLOAD_OBSOLETE_REFUNDING;
                } else {
                    desStatus = PlanStatus.ABORTED_REFUNDING;
                }
            }
        }
        return desStatus;
    }

    public static PlanStatus getRecycleCompletedStatus(PlanStatus srcStatus) {
        if (srcStatus == null) {
            logger.error("传入数据为空");
            return null;
        }

        PlanStatus desStatus = null;

        if (srcStatus.getValue() == PlanStatus.CANCELLING.getValue()) {
            desStatus = PlanStatus.CANCELLED;
        } else if (srcStatus.getValue() == PlanStatus.ABORTED_REFUNDING.getValue()) {
            desStatus = PlanStatus.ABORTED;
        } else if (srcStatus.getValue() == PlanStatus.PRINTED_FAILED_REFUNDING.getValue()) {
            desStatus = PlanStatus.PRINTED_FAILED;
        } else if (srcStatus.getValue() == PlanStatus.UNPRINTED_OBSOLETE_REFUNDING.getValue()) {
            desStatus = PlanStatus.UNPRINTED_OBSOLETED;
        } else if (srcStatus.getValue() == PlanStatus.NOTPAID_OBSOLETING.getValue()) {
            desStatus = PlanStatus.NOTPAID_OBSOLETED;
        } else if (srcStatus.getValue() == PlanStatus.NOTUPLOAD_OBSOLETE_REFUNDING.getValue()) {
            desStatus = PlanStatus.NOTUPLOAD_OBSOLETED;
        }

        return desStatus;
    }
	
	public static final PlanStatus ALL = new PlanStatus("全部", -1);
	public static final PlanStatus DEFAULT = new PlanStatus("默认", 0);

	public static final PlanStatus PAID_NOT = new PlanStatus("未支付", 1);
	public static final PlanStatus RECRUITING = new PlanStatus("招募中", 2);
	public static final PlanStatus PRINT_WAITING = new PlanStatus("待出票", 3);
	public static final PlanStatus PRINTED = new PlanStatus("已出票", 4);
	public static final PlanStatus CANCELLED = new PlanStatus("已撤单", 5);
	public static final PlanStatus ABORTED = new PlanStatus("未满员撤单", 6);
	public static final PlanStatus PRINTING = new PlanStatus("正在出票", 7);
	public static final PlanStatus PRINTED_PARTIAL = new PlanStatus("部分出票", 8);
	public static final PlanStatus UNPRINTED_OBSOLETED = new PlanStatus("未出票作废", 9);
	public static final PlanStatus PRINTED_FAILED = new PlanStatus("出票失败", 10);
	public static final PlanStatus CANCELLING = new PlanStatus("正在撤单", 11);		//client方案发起人合买撤单时，标记成此状态，由engine线程处理撤单返款
	public static final PlanStatus ABORTED_REFUNDING = new PlanStatus("未满员撤单返款中", 12);
	public static final PlanStatus PRINTED_FAILED_REFUNDING = new PlanStatus("出票失败返款中", 13);
	public static final PlanStatus UNPRINTED_OBSOLETE_REFUNDING = new PlanStatus("未出票作废返款中", 14);
	public static final PlanStatus NOTPAID_OBSOLETED = new PlanStatus("未支付作废", 15);
	public static final PlanStatus NOTPAID_OBSOLETING = new PlanStatus("未支付作废中", 16);
    public static final PlanStatus NOTUPLOAD_OBSOLETED = new PlanStatus("未上传作废", 17);
    public static final PlanStatus NOTUPLOAD_OBSOLETE_REFUNDING = new PlanStatus("未上传作废返款中", 18);
}
